﻿using Mododor.Ext;
using Mododor.JsonModels;
using Mododor.Models;
using Mododor.ViewInterfaces;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mododor.ViewModels.CompanyInfoViewModels
{
    class ClientInfoViewModel : ViewInterface<ClientInfoModel>, INavigationAware
    {
        private ClientInfoModel clientInfoModel;

        public ClientInfoModel ClientInfoModel
        {
            get { return clientInfoModel; }
            set { clientInfoModel = value; RaisePropertyChanged(); }
        }
        private CompanyInfoModel companyInfo;

        public CompanyInfoModel CompanyInfo
        {
            get { return companyInfo; }
            set { companyInfo = value; RaisePropertyChanged(); }
        }

        public DelegateCommand CompanySelectionChanged { get; set; }
        public ClientInfoViewModel(IDialogService dialog, HttpHelper httpHelper) : base(dialog, httpHelper)
        {
            DataStatsValueList = new ObservableCollection<ClientInfoModel>();

            SaveDataCommand = new DelegateCommand(async () =>
            {
                ClientInfoModel.Client_IdCompany = CompanyInfo.Id;
                string json = JsonConvert.SerializeObject(ClientInfoModel);
                string res = await _httpHelper.PostJson(ExtUrl.ClientOperationUrl, json);
                JustHaveStausAndMsgInfoJsonModel.Rootobject root = JsonConvert.DeserializeObject<JustHaveStausAndMsgInfoJsonModel.Rootobject>(res);
                SnackBarMessage = root.msg;
                await GetCompanyList();
            });

            ClearDataCommand = new DelegateCommand(() =>
            {
                ClientInfoModel = new ClientInfoModel();
            });

            MarkingDeleteCommand = new DelegateCommand(() =>
            {
                if (CompanyInfo is null)
                {
                    return;
                }

                DialogParameters dialogPara = new DialogParameters()
                {
                    {"Key","确认是否删除" }
                };

                _dialogService.ShowDialog("AskingDialogView",dialogPara, async x =>
                {
                    if (x.Result == ButtonResult.OK)
                    {
                        string json = await _httpHelper.GetJson($"{ExtUrl.ClientMarkingDeleteUrl}?clientId={ClientInfoModel.idClient}");
                        JustHaveStausAndMsgInfoJsonModel.Rootobject root = JsonConvert.DeserializeObject<JustHaveStausAndMsgInfoJsonModel.Rootobject>(json);
                        SnackBarMessage = root.msg;
                        await GetCompanyList();
                    }
                }, "DialogWindowView");
            });

            CompanySelectionChanged = new DelegateCommand(async () =>
            {
                await 获取指定公司员工信息();
            });
        }

        protected override async Task GetCompanyList()
        {
            try
            {
                await base.GetCompanyList();
            }
            catch (Exception ex)
            {
                SnackBarMessage = ex.Message;
                await SnackBarWaiting();
            }
        }
        protected override async Task SnackBarWaiting()
        {
            await base.SnackBarWaiting();
        }

        async Task 获取指定公司员工信息()
        {
            ClientInfoModel = new ClientInfoModel();
            IsOpen = true;
            DataStatsValueList.Clear();
            if (CompanyInfo != null)
            {
                string json = await _httpHelper.GetJson($"{ExtUrl.ClientSearchAllUrl}?idCompany={CompanyInfo.Id}");
                ClientInfoJsonSearchModel.Rootobject root = JsonConvert.DeserializeObject<ClientInfoJsonSearchModel.Rootobject>(json);
                foreach (var item in root.res)
                {
                    DataStatsValueList.Add(new ClientInfoModel()
                    {
                        ClientEmail = item.clientEmail,
                        ClientJobTitle = item.clientJobTitle,
                        ClientMobile = item.clientMobile,
                        ClientNameCh = item.clientNameCh,
                        ClientNameEn = item.clientNameEn,
                        CompanyNameCh = item.companyNameCh,
                        Client_IdCompany = item.idCompany,
                        idClient = item.idClient
                    });
                }
                SnackBarMessage = root.msg;
            }
            IsOpen = false;
            await SnackBarWaiting();
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public async void OnNavigatedTo(NavigationContext navigationContext)
        {
            await GetCompanyList();
        }
    }
}
